package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.ReturnCauseMapper;
import org.glow.changgou.order.pojo.OrderItem;
import org.glow.changgou.order.pojo.OrderLog;
import org.glow.changgou.order.pojo.ReturnCause;
import org.glow.changgou.order.service.ReturnCauseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:ReturnCause业务层接口实现类
 *****/
@Service
public class ReturnCauseServiceImpl  extends ServiceImpl<ReturnCauseMapper, ReturnCause> implements ReturnCauseService {

    @Resource
    private ReturnCauseMapper returnCauseMapper;


    /**
     * ReturnCause条件+分页查询
     * @param returnCause 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<ReturnCause> findPage(ReturnCause returnCause, int page, int size){
        Page<ReturnCause> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<ReturnCause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnCause);
        return returnCauseMapper.selectPage(pageInfo,queryWrapper);
    }

    @Override
    public Page<ReturnCause> findPage(int page, int size) {
        Page<ReturnCause> pageInfo = new Page<>(page,size);
        return returnCauseMapper.selectPage(pageInfo,null);
    }


    /**
     * ReturnCause条件查询
     * @param returnCause
     * @return
     */
    @Override
    public List<ReturnCause> findList(ReturnCause returnCause){

        LambdaQueryWrapper<ReturnCause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnCause);
        return returnCauseMapper.selectList(queryWrapper);
    }


    /**
     * ReturnCause构建查询对象
     * @param returnCause
     * @return
     */
    public LambdaQueryWrapper<ReturnCause> getCondition(LambdaQueryWrapper<ReturnCause> queryWrapper, ReturnCause returnCause){

        if(returnCause!=null){
            // ID
            if(!StringUtils.isEmpty(returnCause.getId())){
//                    criteria.andEqualTo("id",returnCause.getId());
                    queryWrapper.eq(ReturnCause::getId,returnCause.getId());
            }
            // 原因
            if(!StringUtils.isEmpty(returnCause.getCause())){
//                    criteria.andEqualTo("cause",returnCause.getCause());
                    queryWrapper.eq(ReturnCause::getCause,returnCause.getCause());
            }
            // 排序
            if(!StringUtils.isEmpty(returnCause.getSeq())){
//                    criteria.andEqualTo("seq",returnCause.getSeq());
                    queryWrapper.eq(ReturnCause::getSeq,returnCause.getSeq());
            }
            // 是否启用
            if(!StringUtils.isEmpty(returnCause.getStatus())){
//                    criteria.andEqualTo("status",returnCause.getStatus());
                    queryWrapper.eq(ReturnCause::getStatus,returnCause.getStatus());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        returnCauseMapper.deleteById(id);
    }

    /**
     * 修改ReturnCause
     * @param returnCause
     */
    @Override
    public void update(ReturnCause returnCause){
        returnCauseMapper.updateById(returnCause);
    }

    /**
     * 增加ReturnCause
     * @param returnCause
     */
    @Override
    public void add(ReturnCause returnCause){
        returnCauseMapper.insert(returnCause);
    }

    /**
     * 根据ID查询ReturnCause
     * @param id
     * @return
     */
    @Override
    public ReturnCause findById(Integer id){
        return  returnCauseMapper.selectById(id);
    }

    /**
     * 查询ReturnCause全部数据
     * @return
     */
    @Override
    public List<ReturnCause> findAll() {
        return returnCauseMapper.selectList(null);
    }
}
